package com.sls.web.manager.recharge.service.impl;

import java.util.List;
import java.util.UUID;

import org.springframework.stereotype.Service;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.sls.core.common.CommonUtil;
import com.sls.core.socket.Head;
import com.sls.core.socket.SocketDTO;
import com.sls.core.socket.TransmissionCode;
import com.sls.web.manager.common.Constants;
import com.sls.web.manager.execption.ManagerException;
import com.sls.web.manager.page.Pagination;
import com.sls.web.manager.pojo.UserInfoVO;
import com.sls.web.manager.recharge.pojo.ProductVO;
import com.sls.web.manager.recharge.pojo.ProductView;
import com.sls.web.manager.recharge.querybean.ProductQuery;
import com.sls.web.manager.recharge.service.ProductService;
import com.sls.web.manager.socket.SuperService;

@Service("productService")
public class ProductServiceImpl extends SuperService implements ProductService {
	@Override
	public boolean addProduct(ProductVO product, UserInfoVO userinfo) {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.ADD_PRODUCT);
		product.setUpdateOperator(userinfo.getUserName());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, product);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public Pagination<ProductVO> queryProduct(ProductQuery rQuery,
			UserInfoVO userinfo) {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SELECT_PRODUCT_LIST);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, rQuery);
		String body = dto.getBody();
		Pagination<ProductVO> product = gson.fromJson(body,
				new TypeToken<Pagination<ProductVO>>() {
				}.getType());

		return product;
	}

	@Override
	public boolean delete(String id, UserInfoVO userinfo) {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.DEL_PRODUCT);
		ProductVO pr = new ProductVO();
		pr.setId(id);
		pr.setUpdateOperator(userinfo.getUserName());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, pr);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public ProductVO queryProductById(String id, UserInfoVO userinfo) {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.SELECT_PRODUCT_BY_ID);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		ProductVO pr = new ProductVO();
		pr.setId(id);
		SocketDTO dto = super.send(head, pr);
		String body = dto.getBody();
		ProductVO product = gson.fromJson(body, ProductVO.class);
		return product;
	}

	@Override
	public boolean updateProduct(ProductVO product, UserInfoVO userinfo) {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setRequestId(UUID.randomUUID().toString());
		head.setServiceCode(Constants.EDIT_PRODUCT);
		product.setUpdateOperator(userinfo.getUserName());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head, product);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			throw new ManagerException(dto.getHead().getResponseMessage());
	}

	@Override
	public boolean checkName(ProductVO product, UserInfoVO userinfo) {
		String ip = userinfo.getCurrentIp();
		Head head = new Head();
		head.setIp(ip);
		head.setServiceCode(Constants.CHECK_PRODUCT_NAME);
		head.setUserId(userinfo.getUserId());
		product.setName(product.getName());
		product.setDeleted(false);
		SocketDTO dto = super.send(head, product);
		if (TransmissionCode.SUCCESS.getCode().equals(
				dto.getHead().getResponseCode()))
			return true;
		else
			return false;
	}

	@Override
	public List<ProductView> queryProductViewById(String id,UserInfoVO userinfo) {
		Gson gson = CommonUtil.gson();
		Head head = new Head();
		head.setServiceCode(Constants.VIEW_PRODUCT_BY_ID);
		ProductVO pr = new ProductVO();
		pr.setId(id);
		head.setIp(userinfo.getCurrentIp());
		head.setUserId(userinfo.getUserId());
		SocketDTO dto = super.send(head,pr);
		List<ProductView> saList = gson.fromJson(dto.getBody(),
				new TypeToken<List<ProductView>>() {
				}.getType());
		return saList;
	}
}
